define(function (require, exports, module) {
    'use strict';
    var rk = require('rk');
    require('./headlineDialogCtrl');

    $.widget('rk.prmHeadlineCtrl', {
        _create: function () {
            var me = this;
            var elem = me.element;
            me._bindHandler();
        },
        _init: function () {
            var me = this;
            var elem = me.element;
            me._loadData();
        },
        _loadData: function (pageNo) {
            var me = this;
            var elem = me.element;
            if (pageNo == null) {
                pageNo = 1;
            }
            rk.post("/json/oa_prm_content/content-list.action", {
                pageNo: pageNo
            }, function (json) {
                if (json.status == 0) {
                    me.data = json.data;
                    var html = rk.templateText(require('../../../tmpl/index/prm/tpl_prm_list.tpl'), me.data);
                    elem.html(html);
                    elem.find('[act=create_headline]').beTip({dir: 'left'});
                    me._initPage(me.data);
                }
            }).fail(function (def, code, message) {
                def.status != 0 && $.msg(rk.i18n('REQUEST_ERROR'));
            });
        },
        _initPage: function (data) {
            var me = this;
            var elem = me.element;
            elem.find('div.prm_content_pagination').rkPaging({
                hideForward: 1,
                pagenum: data.pageNo - 0,
                pagesize: data.pageSize - 0,
                rowscount: data.totalCount - 0,
                onChange: function (pageNo) {
                    me._loadData(pageNo);
                }
            });
        },
        _bindHandler: function () {
            var me = this;
            var elem = me.element;
            //点击新建
            elem.on('click', 'a[act=create_headline]', function () {
                rk.dialog({
                    title: rk.i18n('PAGE_CREATE_HEADLINE'),
                    width: "930",
                    success: function (contentElem) {
                        contentElem.headlineDialogCtrl();
                    },
                    footer: ['cancel', {
                        act: 'draft',
                        visible: true,
                        text: '保存为草稿'
                    }, {
                        act: 'ok',
                        visible: true,
                        text: rk.i18n('APPS_FEED_RELEASE')
                    }]
                });
            });
            //点击编辑
            elem.on('click', 'a.js-prm-edit', function () {
                var $lnk = $(this);
                rk.dialog({
                    title: rk.i18n('PAGE_CREATE_HEADLINE'),
                    width: "930",
                    success: function (contentElem) {
                        contentElem.headlineDialogCtrl({
                            contentId: $lnk.attr('contentId')
                        });
                    },
                    footer: ['cancel', {
                        act: 'draft',
                        visible: true,
                        text: '保存为草稿'
                    }, {
                        act: 'ok',
                        visible: true,
                        text: rk.i18n('APPS_FEED_RELEASE')
                    }]
                });
            });
            //点击删除
            elem.on('click', 'a.js-prm-delete', function () {
                var $lnk = $(this);
                rk.confirm('确定删除头条？', function () {
                    $.post('/json/oa_prm_content/delete-content.action', {
                        contentId: $lnk.attr('contentId')
                    }, null, 'json').done(function (json) {
                        if (json.status == 0) {
                            $("#index_prm_panel").prmHeadlineCtrl();
                            $.msg(rk.i18n('TASK_DELETE_SUCCESS'), 1);
                        } else {
                            $.msg(rk.i18n('TASK_DELETE_ERROR'));
                        }
                    }).fail(function (def, code, message) {
                        def.status != 0 && $.msg(rk.i18n('LOAD_ERROR'));
                    });
                });
            });
        }
    });
});